home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / WildCardFilenameFilter.java < prev    next >
Text File  |  1998-09-08  |  2KB  |  119 lines

  1. package com.symantec.itools.io;
  2.  
  3.  
  4. import java.io.File;
  5. import java.io.FilenameFilter;
  6. import java.io.IOException;
  7. import com.symantec.itools.lang.StringUtils;
  8.  
  9.  
  10. /**
  11.  * @author Symantec Internet Tools Division
  12.  * @version 1.0
  13.  * @since VCafe 3.0
  14.  */
  15.  
  16. public class WildCardFilenameFilter
  17.     implements FilenameFilter
  18. {
  19.  
  20.     /**
  21.      * @since VCafe 3.0
  22.      */
  23.     protected String  pattern;
  24.  
  25.     /**
  26.      * @since VCafe 3.0
  27.      */
  28.     protected boolean caseSensitive;
  29.  
  30.     public WildCardFilenameFilter()
  31.     {
  32.         this("*");
  33.     }
  34.  
  35.     public WildCardFilenameFilter(String pattern)
  36.     {
  37.         this(pattern, true);
  38.     }
  39.  
  40.     public WildCardFilenameFilter(String pat, boolean f)
  41.     {
  42.         pattern       = pat;
  43.         caseSensitive = f;
  44.     }
  45.  
  46.     /**
  47.      * @param dir TODO
  48.      * @param name TODO
  49.      * @since VCafe 3.0
  50.      */
  51.  
  52.     public boolean accept(File dir, String name)
  53.     {
  54.         return (matchFilename(name));
  55.     }
  56.  
  57.     /**
  58.      * @param name TODO
  59.      * @since VCafe 3.0
  60.      */
  61.  
  62.     protected boolean matchFilename(String name)
  63.     {
  64.         int    loc;
  65.         String pre;
  66.         String post;
  67.  
  68.         // common patterns
  69.         if(pattern.equals("*") || pattern.equals("*.*"))
  70.         {
  71.             return (true);
  72.         }
  73.  
  74.         loc = pattern.indexOf('*');
  75.  
  76.         if(loc == -1)
  77.         {
  78.             if(caseSensitive)
  79.             {
  80.                 return (pattern.equals(name));
  81.             }
  82.  
  83.             return (pattern.equalsIgnoreCase(name));
  84.         }
  85.  
  86.         if(loc == 0)
  87.         {
  88.             return (name.endsWith(pattern.substring(1, pattern.length())));
  89.         }
  90.  
  91.         pre  = pattern.substring(0, loc);
  92.         post = pattern.substring(loc + 1, pattern.length());
  93.  
  94.         if(caseSensitive)
  95.         {
  96.             if(!(name.startsWith(pre)))
  97.             {
  98.                 return (false);
  99.             }
  100.  
  101.             if(!(name.endsWith(post)))
  102.             {
  103.                 return (false);
  104.             }
  105.         }
  106.  
  107.         if(!(StringUtils.startsWithIgnoreCase(name, pre)))
  108.         {
  109.             return (false);
  110.         }
  111.  
  112.         if(!(StringUtils.endsWithIgnoreCase(name, post)))
  113.         {
  114.             return (false);
  115.         }
  116.  
  117.         return (true);
  118.     }
  119. }